import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
const DB = require('../../../app/db');
import Utils from './../../../utils'
import update from 'react-addons-update';
import StorageUtils from './../../../app/storage'
import nattyStorage from 'natty-storage';

const storageEngs = nattyStorage({
  type: 'localStorage',  // 缓存方式, 默认为'localStorage'
  key: 'sskj-dingtalk-engs',     // !!! 唯一必选的参数, 用于内部存储 !!!
});

export default Reflux.createStore({
    listenables: [Actions],
    data: {
        ...PageConst,
      lastPage: false,
      keyword: '',
      currentPage: 1,
      pageSize: 1,
      jlmap : new Map(),
      engid:'',
      //选中的工程
      currentEng: {},
      userInfo: {
        certNum: '',
        telNum: '13972608122',
        vcode: ''
      }

    },

  onSetCurrentEng: function (obj) {


    this.data.currentEng = update(this.data.currentEng, {$set: obj});
    let resultOid = Utils.isEmptyObject(this.data.currentEng.oid);
    if (!resultOid.success)
    {

      this.data.engid = update(this.data.engid, {$set: this.data.currentEng.id});

    }
    else {
      this.data.engid = update(this.data.engid, {$set: this.data.currentEng.oid});

    }


    this.data.engid = update(this.data.engid, {$set: this.data.currentEng.id});

    this.onSbazFetch({
      page: 1,
      size: 50,
      keyword:'',
      userid:'13972608122',
      engid:'',
      rq:'',
    });
    this.onYbbyFetch({
        page: 1,
        size: 50,
        keyword:'',
        userid:'13972608122',
        engid:'',
        rq:'',
      });
      this.onJgsqFetch({
        page: 1,
        size: 50,
        keyword:'',
        userid:'13972608122',
        engid:'',
        rq:'',
      });
    this.updateComponent();
  },

  initMode: function () {
    this.data.currentEng =  update(this.data.currentEng,  {$set: {}});

    this.data.sbaz = update(this.data.sbaz, {seng: {$set: {}}});
    this.data.sbaz = update(this.data.sbaz, {jlrq: {$set: {}}});
    this.data.sbaz = update(this.data.sbaz, {reljl:  {$set: []}});


    this.data.ybby = update(this.data.ybby, {seng: {$set: {}}});
    this.data.ybby = update(this.data.ybby, {jlrq: {$set: {}}});
    this.data.ybby = update(this.data.ybby, {reljl:  {$set: []}});

    this.data.jgsq = update(this.data.jgsq, {seng: {$set: {}}});
    this.data.jgsq = update(this.data.jgsq, {jlrq: {$set: {}}});
    this.data.jgsq = update(this.data.jgsq, {reljl:  {$set: []}});

    this.data.jlmap.set("设备安装", this.data.sbaz);
    this.data.jlmap.set("隐蔽报验", this.data.ybby);
    this.data.jlmap.set("竣工申请", this.data.jgsq);

    this.data.engid = update(this.data.engid, {$set: ''});
  },

  initJDJL: function () {
    let t = this;
/*    Actions.loadSBAZ(true);
    Actions.loadYBBY(true);
    Actions.loadJGSQ(true);*/

    this.onSbazFetch({
      page: 1,
      size: 50,
      keyword: '',
      userid: '13972608122',
      engid: '',
      jllx:'0008',
      rq: '',
    });

    this.onJgsqFetch({
      page: 1,
      size: 50,
      keyword: '',
      userid: '13972608122',
      engid: '',
      jllx:'0008',
      rq: '',
    });

    this.onYbbyFetch({
      page: 1,
      size: 50,
      keyword: '',
      userid: '13972608122',
      engid: '',
      jllx:'0008',
      rq: '',
    });
    t.updateComponent();
  },

  onLoadSBAZ: function (model) {

    this.onSbazFetch({
      page: 1,
      size: 50,
      keyword: '',
      userid: '13972608122',
      engid: '',
      jllx:'0008',
      rq: '',
    });
  },

  onLoadYBBY: function (model) {

    this.onYbbyFetch({
      page: 1,
      size: 50,
      keyword: '',
      userid: '13972608122',
      engid: '',
      jllx:'0008',
      rq: '',
    });
  },

  onLoadJGSQ: function (model) {

    this.onJgsqFetch({
      page: 1,
      size: 10,
      keyword: '',
      userid: '13972608122',
      engid: '',
      jllx:'0008',
      rq: '',
    });
  },


  onJgsqFetch: function (params) {
    let t = this;
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.keyword = t.data.keyword;
    params.engid = t.data.engid;
    let resultEng = Utils.isEmptyObject(this.data.currentEng);
    if (resultEng.success)
    {
      t.data.jgsq = update(t.data.jgsq, {seng: {$set: this.data.currentEng}});
      t.data.jgsq = update(t.data.jgsq, {jlrq: {$set: {}}});
    }
    DB.WechatAPI.findJGSQControl(params).then(
      function (content) {


        if (content != null && content.content != null) {

          t.data.jgsq = update(t.data.jgsq, {relObj: {$set: content.content[0]}, reljl: {$set: content.content}});

          t.data.jgsq = update(t.data.jgsq, {seng: {$set: t.data.jgsq.relObj.eng}});
          t.data.jgsq = update(t.data.jgsq, {jlrq: {$set: t.data.jgsq.relObj.jlrq}});

          t.data.jlmap.set("竣工申请", t.data.jgsq);

        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
  },

  onYbbyFetch: function (params) {

    let t = this;
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.keyword = t.data.keyword;
    params.engid = t.data.engid;
    let resultEng = Utils.isEmptyObject(this.data.currentEng);
    if (resultEng.success)
    {
      t.data.ybby = update(t.data.ybby, {seng: {$set: this.data.currentEng}});
      t.data.ybby = update(t.data.ybby, {jlrq: {$set: {}}});
    }
    DB.WechatAPI.findYBBYControl(params).then(
      function (content) {
        if (content != null && content.content != null) {

          t.data.ybby = update(t.data.ybby, {relObj: {$set: content.content[0]}, reljl: {$set: content.content}});

          t.data.ybby = update(t.data.ybby, {seng: {$set: t.data.ybby.relObj.eng}});
          t.data.ybby = update(t.data.ybby, {jlrq: {$set: t.data.ybby.relObj.jlrq}});
          t.data.jlmap.set("隐蔽报验", t.data.ybby);

          t.updateComponent();
        }

      },
      function (error) {
        console.log(error);
      });
  },

  onSbazFetch: function (params) {
    let t = this;
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.keyword = t.data.keyword;
    params.engid = t.data.engid;
    let resultEng = Utils.isEmptyObject(this.data.currentEng);
    if (resultEng.success)
    {
      t.data.sbaz = update(t.data.sbaz, {seng: {$set: this.data.currentEng}});
      t.data.sbaz = update(t.data.sbaz, {jlrq: {$set: {}}});
    }
    DB.WechatAPI.findSBAZControl(params).then(
      function (content) {

        if (content != null && content.content != null) {

          t.data.sbaz = update(t.data.sbaz, {relObj: {$set: content.content[0]}, reljl: {$set: content.content}});

          t.data.sbaz = update(t.data.sbaz, {seng: {$set: t.data.sbaz.relObj.eng}});
          t.data.sbaz = update(t.data.sbaz, {jlrq: {$set: t.data.sbaz.relObj.jlrq}});

          t.data.jlmap.set("设备安装", t.data.sbaz);

          t.updateComponent();
        }

      },
      function (error) {
        console.log(error);
      });
    t.updateComponent();
  },



  updateComponent: function () {
        this.trigger(this.data);
  },

  getDefaultProps: function () {

  },

  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  getInitialState: function () {
        return this.data;
  }
});
